/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.UserPrivileges;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.ArrayList;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class LoginPanel extends Content {
	private CatalogMessages cons = null;
	private GwtContext context = null;
	private BifangGWTServiceAsync service;
	private TextBox userName;
	private PasswordTextBox password;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				if(code.equals("-1")){
					Window.alert(cons.loginError());
					return;
				}
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			UserPrivileges up = new UserPrivileges();
			up.setRole("admin");
			context.put(up.USER_PRIVILEGES, up);
			listener.onContentChanged(CatalogContentConstants.LOGIN_PANEL,
					CatalogContentConstants.MAIN_PANEL, null);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public LoginPanel(CatalogMessages cons, BifangGWTServiceAsync service1,
			GwtContext context) {
		this.cons = cons;
		this.service = service1;
		this.context = context;

		DockPanel dock = new DockPanel();
		dock.setSpacing(4);
		dock.setHorizontalAlignment(DockPanel.ALIGN_CENTER);

		// Add text all around
		dock.add(new HTML("&nbsp;"), DockPanel.NORTH);
		dock.add(new HTML("&nbsp;"), DockPanel.SOUTH);
		dock.add(new HTML("&nbsp;"), DockPanel.EAST);
		dock.add(new HTML("&nbsp;"), DockPanel.WEST);

		// Add scrollable text in the center
		VerticalPanel panel = new VerticalPanel();

		panel.setSize("400px", "100px");
		dock.add(panel, DockPanel.CENTER);

		Grid table = new Grid(2, 2);
		table.setWidth("100%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "40%");
		cf.setWidth(1, "60%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");

		table.setHTML(0, 0, cons.userName());
		table.setHTML(1, 0, cons.password());

		userName = new TextBox();
		userName.setVisibleLength(32);

		password = new PasswordTextBox();
		password.setVisibleLength(32);

		table.setWidget(0, 1, userName);
		table.setWidget(1, 1, password);

		panel.add(table);
		panel.add(new HTML("&nbsp;"));

		HorizontalPanel h = new HorizontalPanel();
		Button login = new Button(cons.loginAsAdmin());
		login.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				if (!validate()) {
					return;
				}
				ArrayList<String> param = new ArrayList<String>();
				param.add(userName.getText());
				if (StringUtils.isEmpty(password.getText())) {
					param.add("");
				} else {
					param.add(password.getText());
				}

				RequestObject req = new RequestObject();
				req.setAction("han.bifang.gwt.server.action.login.LoginAction");
				req.setRequest(param);

				service.call(req, cb);

			}
		});

		Button view = new Button(cons.viewAsDeveloper());
		view.addClickListener(new ClickListener() {

			public void onClick(Widget sender) {
				listener.onContentChanged(CatalogContentConstants.LOGIN_PANEL,
						CatalogContentConstants.MAIN_PANEL, null);

			}
		});
		h.add(login);
		h.add(new HTML("&nbsp;"));
		h.add(view);
		panel.add(h);
		
		initWidget(dock);
	}

	private boolean validate() {
		if (StringUtils.isEmpty(userName.getText())) {
			Window.alert(cons.cannotNull(cons.userName()));
			return false;
		}
		
		if (StringUtils.isEmpty(password.getText())) {
			Window.alert(cons.cannotNull(cons.password()));
			return false;
		}

		return true;

	}
}
